<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Gregwar\Captcha\CaptchaBuilder;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
   //加载登录模板
   public function login()
   {
       return view("admin.login");
   }
   
   //加载验证码
   //执行验证码
		 function getcode(){
			$builder = new CaptchaBuilder;
			$builder->build(150,32);
			\Session::set('phrase',$builder->getPhrase()); //存储验证码
			return response($builder->output())->header('Content-type','image/jpeg');
		}
   

   //执行用户登录
   public function doLogin(Request $request)
   {	
	   //执行登陆判断
        $email = $request->input("email");
        $password = $request->input("password");
		
		
		//判断验证码是否正确
		$code=$request->input("code");
		$yanzhenma=$request->session()->get("phrase");
		if($code !== $yanzhenma){
			return back()->with("msg","验证码输入错误！");
			
		}
		
        //获取对应用户信息
        $user = \DB::table("users")->where("email",$email)->first();
        if(!empty($user)){
            //判断密码
            if(md5($password)==$user->password){
                //存储session跳转页面
                session()->set("adminuser",$user);
                
                return redirect("admin");
               //echo "测试成功!";
            }
        }
        return back()->with("msg","账号或密码错误！"); 
       
   }
   
   //加载验证码
   
   //执行退出
	public function logout(Request $request)
   {
       $request->session()->forget('adminuser');
       return redirect("admin/login");
   } 
}
